
jQuery(document).ready(function ($) {
    //$.ajax({
    //    url:'http://localhost:3000/users/api',
    //    success:function(res){
    //        var tr = '';
    //        for(var i=0;i<res.length;i++)
    //        {
    //
    //            var obj = JSON.parse(res[i]);
    //            var info = JSON.stringify(obj.info);
    //            //console.log(obj.info);
    //            tr+='<tr> <td> <div class="task-chk-wrap"> <div class="checkbox-inline"> <div class="custom-checkbox font-12"> <input type="checkbox" class="library-chk" id="task2" name="task2"> <label for="task2"></label> </div> </div> </div> </td> <td class="text-center">'+obj.title+'</td> <td class="text-center">'+obj.category+'</td> <td class="text-center" data-info="'+info+'">'+obj.count+'</td> <td class="text-center">'+obj.click+'</td> <td class="text-center"><a href="#" class="text-btn text-info">编辑</a> <a href="#" class="text-btn text-danger">删除</a> </td> </tr>';
    //        }
    //
    //        $(".databody").html(tr);
    //    }
    //});
    /*---------------------------------------
     公用弹出框
     ---------------------------------------*/

    $('.common-po-btn').on('click', function (event) {
        event.preventDefault();
        $('.common-alert').addClass('is-visible');
    });

    //close popup
    $('.common-alert').on('click', function (event) {
        if ($(event.target).is('.cd-popup-close') || $(event.target).is('.common-alert')) {
            event.preventDefault();
            $(this).removeClass('is-visible');
        }
    });

    //close popup when clicking the esc keyboard button
    $(document).keyup(function (event) {
        if (event.which == '27') {
            $('.common-alert').removeClass('is-visible');
        }
    });


    /*---------------------------------------
     点击量弹出框
     ---------------------------------------*/

    $('.click-po-btn').on('click', function (event) {
        event.preventDefault();
        $('.click-alert').addClass('is-visible');
    });

    //close popup
    $('.click-alert').on('click', function (event) {
        if ($(event.target).is('.cd-popup-close') || $(event.target).is('.click-alert')) {
            event.preventDefault();
            $(this).removeClass('is-visible');
        }
    });

    //close popup when clicking the esc keyboard button
    $(document).keyup(function (event) {
        if (event.which == '27') {
            $('.click-alert').removeClass('is-visible');
        }
    });

    /*---------------------------------------
     删除量弹出框
     ---------------------------------------*/

    $('.delete-po-btn').on('click', function (event) {
        event.preventDefault();
        $('.delete-alert').addClass('is-visible');
    });

    //close popup
    $('.delete-alert').on('click', function (event) {
        if ($(event.target).is('.cd-popup-close') || $(event.target).is('.delete-alert')) {
            event.preventDefault();
            $(this).removeClass('is-visible');
        }
    });

    //close popup when clicking the esc keyboard button
    $(document).keyup(function (event) {
        if (event.which == '27') {
            $('.click-alert').removeClass('is-visible');
        }
    });


    /*---------------------------------------
     select下拉菜单
     ---------------------------------------*/
    $('.chosen-select').select2();


    /*---------------------------------------
     图片上传
     ---------------------------------------*/

    $("#file-4").fileinput({
        uploadExtraData: {kvId: '10'}
    });


    /*---------------------------------------
     下拉选框
     ---------------------------------------*/
    $('#jstree2').jstree({'plugins': ["wholerow", "checkbox"], 'core': {
        'data': [
            {
                "text": "Same but with checkboxes",
                "children": [
                    { "text": "initially selected", "state": { "selected": true } },
                    { "text": "custom icon URL", "icon": "//jstree.com/tree-icon.png" },
                    { "text": "initially open", "state": { "opened": true }, "children": [ "Another node" ] },
                    { "text": "custom icon class", "icon": "glyphicon glyphicon-leaf" }
                ]
            },
            "And wholerow selection"
        ]
    }});

    $('#jstree3').jstree({'plugins': ["wholerow", "checkbox"], 'core': {
        'data': [
            {
                "text": "Same but with checkboxes",
                "children": [
                    { "text": "initially selected", "state": { "selected": true } },
                    { "text": "custom icon URL", "icon": "//jstree.com/tree-icon.png" },
                    { "text": "initially open", "state": { "opened": true }, "children": [ "Another node" ] },
                    { "text": "custom icon class", "icon": "glyphicon glyphicon-leaf" }
                ]
            },
            "And wholerow selection"
        ]
    }});


    /*---------------------------------------
     radio check
     ---------------------------------------*/


    $(function () {
        showCont();
        var showRadio = $('input[name=inlineRadioOptions]');
        showRadio.click(function () {
            showCont();
        });
    });

    function showCont() {
        var showRadioId = $('input[name=inlineRadioOptions]:checked').attr("id");
        var textShow1 = $("#textShow1");
        var textShow2 = $("#textShow2");
        switch (showRadioId) {
            case 'price_type1':
                textShow2.hide();
                textShow1.show();
                break;
            case 'price_type2':
                textShow1.hide();
                textShow2.show();
                break;
            default:
                break;

        }
    }


    $(function () {
        showUnique();
        var showUniquebtn = $('input[name=inlineRadioOptions]');
        showUniquebtn.click(function () {
            showUnique();
        });
    });

    function showUnique() {
        var showIniqueId = $('input[name=inlineRadioOptions]:checked').attr('id');
        var uniqueCheck = $("#uniqueCheck");
        switch (showIniqueId) {
            case "uniqueRadio1":
                uniqueCheck.hide();
                break;
            case "uniqueRadio2":
                uniqueCheck.show();
                break;
            default :
                break;
        }
    }


    $(function(){
        showCheck();
        var showCheckbtn = $('input[name=checkbox-btn]');
        showCheckbtn.click(function(){
            showCheck();
        });
    });

    function showCheck(){
        var showCehckName = $('input[name=checkbox-btn]:checked').attr('class');
        var checkcontent = $("#check-container");
        console.log(showCehckName);
        if(showCehckName == 'checkbox-btn')
        {
            checkcontent.removeClass('hidden');
        }
        else
        {
            checkcontent.addClass('hidden');
        }

    }

    /*---------------------------------------
     article all checked and all unchecked
     ---------------------------------------*/

    $(".task-chk").click(function () {
        var name = $(this).prop("name");
        if (!name.match(/all/))
            return;

        var div = $(this).parents(".task-items");
        var checkbox = div.find(".task-chk");
        var checked = $(this).prop("checked");

        for (var i = 0; i < checkbox.length - 1; i++) {
            //console.log(checked,i,checkbox);
            if (checked) {
                checkbox.eq(i).prop({"checked": true});
            }
            else {
                checkbox.eq(i).prop({'checked': false});

            }
        }
    });

    /*---------------------------------------
     table all checked and all unchecked
     ---------------------------------------*/

    $(".table-task").click(function () {
        var name = $(this).prop("name");
        if (!name.match(/equibtn/)) {
            return;
        }
        console.log($(this));
        var div = $(this).parents(".checkcontainer");
        var checkbox = div.find(".task-chk");
        var checked = $(this).prop("checked");

        for (var i = 0; i < checkbox.length; i++) {
            console.log(checked, i, checkbox);
            if (checked) {
                checkbox.eq(i).prop({"checked": true});
            }
            else {
                checkbox.eq(i).prop({"checked": false})
            }
        }

    });

    /*---------------------------------------
     library all checked and all unchecked
     ---------------------------------------*/

    $(".library-chk").click(function () {
        var name = $(this).prop("name");
        if (!name.match(/all/))
            return;

        var div = $(this).parents(".table-items");
        var checkbox = div.find(".library-chk");
        var checked = $(this).prop("checked");

        for (var i = 1; i < checkbox.length; i++) {
            console.log(checked, i, checkbox);
            if (checked) {
                checkbox.eq(i).prop({"checked": true});
            }
            else {
                checkbox.eq(i).prop({'checked': false});

            }
        }
    });


    /*---------------------------------------
     popover
     ---------------------------------------*/

    $(function(){
        $('[data-toggle="tooltip"]').tooltip();
        $('[data-toggle="popover"]').popover();
    });
    $(document).ready(function(){
        //自定义内容
        $("#mypopover").popover({
            html:true,
            content: function(){
                return $("#popover-content").html();
            }
        });
    });



    /*---------------------------------------
     library delete
     ---------------------------------------*/

    $(function() {
        showLibrary();
        var library = $(".library");
        var delete_btn = library.find("input");
        delete_btn.click(function() {
            showLibrary();
        });
    });

    function showLibrary(){
        var library = $(".library");
        var delete_btn = library.find("input");
        var lchecked = library.find("input:checked").attr('type');
        var delete_container = $(".delete-container");
        console.log(lchecked);
        delete_btn.click(function(){
            if( lchecked )
            {
                delete_container.removeClass("hidden");
            }
            else
            {
                delete_container.addClass("hidden");
            }
        });
    }

});




